package tom.adminlte.controller;


import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.WebAttributes;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

/**
 * @author ZHUFEIFEI
 */
@Slf4j
@Controller
public class ViewController {

    @GetMapping("index")
    public String index() {
        return "index";
    }

    @GetMapping("welcome")
    public String welcome() {
        return "welcome";
    }

    @GetMapping("login")
    public String login(@RequestParam(value = "error", required = false) String error
            , Model model) {
        if (error != null) {
            String msg = "username or password invalidate!";
            model.addAttribute("error", msg);
        }
        return "login";
    }

    @RequestMapping("403")
    public String forbidden(Model model, HttpServletRequest request) {
        String msg = null;
        if (request.getAttribute(WebAttributes.ACCESS_DENIED_403) != null) {
            //AccessDeniedHandlerImpl
            msg = ((AccessDeniedException) request.getAttribute(WebAttributes.ACCESS_DENIED_403)).getMessage();
            log.warn(msg);
        }
        model.addAttribute("error", msg);
        return "error/403";
    }

}
